package com.kk.common.exception;

/**
 * @author : K k
 * @date : 11:26 2020/6/12
 */

import com.kk.common.lang.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /*//用户被冻结
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(value = IllegalStateException.class)
    public Result handler(IllegalStateException e){
        log.error("运行时异常：------------->",e);
        return Result.fail(e.getMessage());
    }*/

    //权限不足
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(value = AccessDeniedException.class)
    public Result handler(AccessDeniedException e){
        log.error("运行时异常：------------->",e);
        return Result.fail(e.getMessage());
    }

    //用户不存在
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(value = UsernameNotFoundException.class)
    public Result handler(UsernameNotFoundException e){
        log.error("运行时异常：------------->",e);
        return Result.fail(e.getMessage());
    }

    //异常
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    public Result handler(RuntimeException e){
        log.error("运行时异常：------------->",e);
        return Result.fail(e.getMessage());
    }
}
